package com.zhongxi.guoyuan.view.seller;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.seller.BossCenterData;
import com.zhongxi.guoyuan.entity.seller.BossCenterResult;
import com.zhongxi.guoyuan.entity.seller.QueryPeopleResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.personalCenter.RewardCoinOrderActivity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//import android.support.annotation.RequiresApi;

public class SellerManagerOrderActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout linearCoin;
    private TextView tvAllPerson;
    private TextView AllSuperPerson;
    private TextView tvTodaySuperPerson;
    private TextView tvBeautifulStoreInner;
    private TextView tvBeautifulStoreCash;
    private TextView tvBeautifulStoreTodayInner;
    private TextView tvBeautifulStoreTodayCash;
    private TextView tvAllGetCash;
    private TextView tvTodayCash;
    private TextView tvCoinAll;
    private TextView tvCoinToday;
    private TextView tvAllCoin;
    private TextView tvTodayCoin;
    private TextView tvStartTime;
    private TextView tvEndTime;
    private DatePickerDialog dialog;
    private String type;
    private Map<String, String> map = new HashMap<String, String>();
    private String startTime;
    private String endTime;
    private LinearLayout linearBeautifulStore;
    private LinearLayout linearReward;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_seller_manager_order);
        initTitle();
        tvTitle.setText("总管中心");
        linearCoin = (LinearLayout) findViewById(R.id.linearCoin);
        linearBeautifulStore = (LinearLayout) findViewById(R.id.linearBeautifulStore);
        linearReward = (LinearLayout) findViewById(R.id.linearReward);
        tvAllPerson = (TextView) findViewById(R.id.tvAllPerson);
        AllSuperPerson = (TextView) findViewById(R.id.AllSuperPerson);
        tvTodaySuperPerson = (TextView) findViewById(R.id.tvTodaySuperPerson);
        tvBeautifulStoreInner = (TextView) findViewById(R.id.tvBeautifulStoreInner);
        tvBeautifulStoreCash = (TextView) findViewById(R.id.tvBeautifulStoreCash);
        tvBeautifulStoreTodayInner = (TextView) findViewById(R.id.tvBeautifulStoreTodayInner);
        tvBeautifulStoreTodayCash = (TextView) findViewById(R.id.tvBeautifulStoreTodayCash);
        tvAllGetCash = (TextView) findViewById(R.id.tvAllGetCash);
        tvTodayCash = (TextView) findViewById(R.id.tvTodayCash);
        tvAllCoin = (TextView) findViewById(R.id.tvAllCoin);
        tvTodayCoin = (TextView) findViewById(R.id.tvTodayCoin);
        tvCoinAll = (TextView) findViewById(R.id.tvCoinAll);
        tvCoinToday = (TextView) findViewById(R.id.tvCoinToday);

        tvStartTime = (TextView) findViewById(R.id.tvStartTime);
        tvEndTime = (TextView) findViewById(R.id.tvEndTime);
    }

    @Override
    protected void initData() {
        super.initData();
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(now);
        tvStartTime.setText(dateNowStr);
        tvEndTime.setText(dateNowStr);
        startTime = endTime = dateNowStr;
        type = "manager";
        getServer(Url.BOSS_CENTER, null, "", true, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if("manager".equals(type)){
                BossCenterResult result = gson.fromJson(response, BossCenterResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if(null!=result.getData()&&!"".equals(result.getData())) {
                    initCenterData(result.getData());
                }
            }else if("query".equals(type)){
                QueryPeopleResult result = gson.fromJson(response, QueryPeopleResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if(null!=result.getNum()&&!"".equals(result.getNum())){
                    tvTodaySuperPerson.setText(result.getNum()+"人");
                }else{
                    tvTodaySuperPerson.setText("人");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj()))
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            if(9002==result.getCode()){
                SharedPreferences sharedPrefrences = getSharedPreferences(Url.APP_SP, MODE_PRIVATE);
                sharedPrefrences.edit().putString("credentials", "").commit();
                showShortToast("用户登录已失效，请重新登录");
                startAct(SellerManagerLoginActivity.class);
                finish();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 初始化数据
     * @param data
     */
    private void initCenterData(BossCenterData data) {
        try {
            if (null != data.getRegister() && !"".equals(data.getRegister())) {
                tvAllPerson.setText(data.getRegister()+"人");
            } else {
                tvAllPerson.setText("*人");
            }
            if (null != data.getPrivilege() && !"".equals(data.getPrivilege())) {
                AllSuperPerson.setText(data.getPrivilege()+"人");
            } else {
                AllSuperPerson.setText("*人");
            }
            if (null != data.getNowPrivilege() && !"".equals(data.getNowPrivilege())) {
                tvTodaySuperPerson.setText(data.getNowPrivilege()+"人");
            } else {
                tvTodaySuperPerson.setText("*人");
            }
            if (null != data.getAllEarnings_inner() && !"".equals(data.getAllEarnings_inner())) {
                if (null != data.getAllEarnings_inner().getPay() && !"".equals(data.getAllEarnings_inner().getPay())) {
                    tvBeautifulStoreCash.setText(data.getAllEarnings_inner().getPay()+"元");
                } else {
                    tvBeautifulStoreCash.setText("*元");
                }
//                if (null != data.getAllEarnings_inner().getPayinnerprice() && !"".equals(data.getAllEarnings_inner().getPayinnerprice())) {
//                    tvBeautifulStoreInner.setText(data.getAllEarnings_inner().getPayinnerprice()+"美币");
//                } else {
//                    tvBeautifulStoreInner.setText("*美币");
//                }
            }
            if (null != data.getNowEarnings_inner() && !"".equals(data.getNowEarnings_inner())) {
                if (null != data.getNowEarnings_inner().getPay() && !"".equals(data.getNowEarnings_inner().getPay())) {
                    tvBeautifulStoreTodayCash.setText(data.getNowEarnings_inner().getPay()+"元");
                } else {
                    tvBeautifulStoreTodayCash.setText("0元");
                }
//                if (null != data.getNowEarnings_inner().getPayinnerprice() && !"".equals(data.getNowEarnings_inner().getPayinnerprice())) {
//                    tvBeautifulStoreTodayInner.setText(data.getNowEarnings_inner().getPayinnerprice()+"美币");
//                } else {
//                    tvBeautifulStoreTodayInner.setText("*美币");
//                }
            }
            if (null != data.getAllEarnings_silver() && !"".equals(data.getAllEarnings_silver())) {
                tvCoinAll.setText(data.getAllEarnings_silver()+"银币");
            } else {
                tvCoinAll.setText("*银币");
            }
            if (null != data.getNowEarnings_silver() && !"".equals(data.getNowEarnings_silver())) {
                tvCoinToday.setText(data.getNowEarnings_silver()+"银币");
            } else {
                tvCoinToday.setText("*银币");
            }

            if (null != data.getCash_wel() && !"".equals(data.getCash_wel())) {
                if (null != data.getCash_wel().getAll() && !"".equals(data.getCash_wel().getAll())) {
                    tvAllGetCash.setText("总"+data.getCash_wel().getAll()+"元");
                } else {
                    tvAllGetCash.setText("总*元");
                }
                if (null != data.getCash_wel().getToday() && !"".equals(data.getCash_wel().getToday())) {
                    tvTodayCash.setText("今"+data.getCash_wel().getToday()+"元");
                } else {
                    tvTodayCash.setText("今*元");
                }
            }
            if (null != data.getSilver_wel() && !"".equals(data.getSilver_wel())) {
                if (null != data.getSilver_wel().getAll() && !"".equals(data.getSilver_wel().getAll())) {
                    tvAllCoin.setText("总"+data.getSilver_wel().getAll()+"银币");
                } else {
                    tvAllCoin.setText("总*银币");
                }
                if (null != data.getSilver_wel().getToday() && !"".equals(data.getSilver_wel().getToday())) {
                    tvTodayCoin.setText("今"+data.getSilver_wel().getToday()+"银币");
                } else {
                    tvTodayCoin.setText("今*银币");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        linearCoin.setOnClickListener(this);
        linearBeautifulStore.setOnClickListener(this);
        linearReward.setOnClickListener(this);
        tvStartTime.setOnClickListener(this);
        tvEndTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.linearCoin:
                startAct(ManagerCoinSellerActivity.class);
                break;
            case R.id.linearReward:
                startAct(RewardCoinOrderActivity.class);
                break;
            case R.id.linearBeautifulStore:
                Intent intent = new Intent(context, SellerBeautifulStoreOrderActivity.class);
                intent.putExtra("index",1);
                startActivity(intent);
                break;
            case R.id.tvStartTime:
                showDateDialog(0);
                break;
            case R.id.tvEndTime:
                showDateDialog(1);
                break;
        }
    }

//    @RequiresApi(api = Build.VERSION_CODES.N)
    private void showDateDialog(final int index) {
        Calendar c = Calendar.getInstance();
        dialog = new DatePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                String mon = month+1+"";
                String text = year + "-" + mon + "-" + dayOfMonth;
                if(index == 0){
                    tvStartTime.setText(text);
                    startTime = text;
                }else if(index == 1){
                    tvEndTime.setText(text);
                    endTime = text;
                }
                getQuery();
            }
        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
        dialog.setTitle("请选择时间：");
        dialog.getDatePicker().setCalendarViewShown(false);
        dialog.show();
    }

    /**
     * 某个时间段内权益用户升级人数
     */
    private void getQuery() {
        map.clear();
        map.put("stime", startTime);
        map.put("etime", endTime);
        type = "query";
        getServer(Url.BOSS_RIGHT_USER, map,"", true, null, "post");
    }
}
